import { getnode } from './json-node';
class SelItem {

    constructor(key, cell) {
        this._rawValue = '';
        this.selType = SelItemType.unkown;
        this.key = '';
        this.values = new Array();
        this.cellValue = {};

        this.rawValue = key;
        this.cellValue = cell;
        this.selType = SelItemType.generate(key);
    }

    get rawValue() {
        return this._rawValue;
    }

    set rawValue(value) {
        let value_reg = /(?:<)(\S*?)(?:>)/g;
        let value_tag = '';

        while ((result = value_reg.exec(value)) != null) {
            if (result[1] != undefined) {
                value_tag = result[1];
            }
            if (value_tag.length > 0) {
                this.values = value_tag.split(',');
            } else {
                this.values = new Array();
            }
        }

        this.key = value.replace(value_reg, '');
        this._rawValue = value;
    }

    isMatch(obj) {
        var isMatch = false;
        var cell = undefined;

        if (this.selType == SelItemType.default) {
            obj['__cell__'] = this.cellValue;
            isMatch = true;
            cell = this.cellValue;
            return { isMatch, cell };
        }

        if (this.selType == SelItemType.sections) {
            let section = 'section' + obj['__section__'];
            if (this.values.includes(section)) {
                obj['__cell__'] = this.cellValue;
                isMatch = true;
                cell = this.cellValue;
                return { isMatch, cell };
            }
        }

        let keys = Object.keys(obj);
        for (let key_index in keys) {
            let oneKey = keys[key_index];

            if (oneKey === this.key) {
                let value = obj[oneKey];
                if (this.values.includes(`${value}`)) {
                    obj['__cell__'] = this.cellValue;
                    isMatch = true;
                    cell = this.cellValue;
                }
            }
        }
        return { isMatch, cell };
    }

}

class SelItemType {

    static ck_priority(typeValue) {
        if (typeValue == SelItemType.cell) {
            return 100;
        }
        if (typeValue == SelItemType.sections) {
            return 90;
        }
        if (typeValue == SelItemType.default) {
            return 80;
        }
        return 10;
    }

    static generate(keyString) {
        if (keyString == '<default>') {
            return SelItemType.default;
        }
        if (keyString.search(/\S+?<\S+?>/g) >= 0) {
            return SelItemType.cell;
        }
        if (keyString.search(/^<section\S+?>/g) >= 0) {
            return SelItemType.sections;
        }
        return SelItemType.sections;
    }
}

SelItemType.default = 'default';
SelItemType.sections = 'sections';
SelItemType.cell = 'cell';
SelItemType.unkown = 'unkown';
class Sections {
    static cellType(row, selector) {
        var item_list = new Array();

        for (sel_key in selector) {
            cellT = selector[sel_key];
            let item = new SelItem(sel_key, cellT);
            item_list.push(item);
        }

        item_list.sort((a, b) => {
            return SelItemType.ck_priority(a.selType) < SelItemType.ck_priority(b.selType);
        });

        var haveLoadCell = false;
        for (sel_index in item_list) {

            let sel = item_list[sel_index];

            if (!haveLoadCell) {
                let { isMatch, cell } = sel.isMatch(row);
                if (isMatch) {
                    haveLoadCell = true;
                    break;
                }
            }
        }
        return row.__cell__;
    }

    static load(sections, selector) {
        let sel_list = new Array();
        for (one in selector) {
            let item = new SelItem(one, selector[one]);
            sel_list.push(item);
        }
        let sectionObj = {};
        for (section_index in sections) {
            section = sections[section_index];
            if (section instanceof Array) {

                console.log('sectionIndex:', section_index);
                let sectionName = 'section' + section_index;
                let oneSection = new Array();
                for (row_index in section) {
                    row = section[row_index];
                    if (row instanceof Object) {
                        row['__row__'] = row_index;
                        row['__section__'] = section_index;

                        Sections.cellType(row, selector);
                    }
                    console.log('Row:', row);

                    oneSection.push(row);
                }
                sectionObj[sectionName] = oneSection;
            }
        }
        return sectionObj;
    }

    static sectionsAppend(sections, sectionIndex, list, selector) {
        let sectionList = getnode(sections, `${sectionIndex}.data`, []);
        let rowIndex = sectionList.length;

        for (row_index in list) {
            row = list[row_index];
            row['__row__'] = row_index + rowIndex;
            row['__section__'] = sectionIndex;
            Sections.cellType(row, selector);
        }
    }

    static sections(datas, selector) {
        let sectionsObj = Sections.load(datas, selector);
        let sections = new Array();
        for (one in sectionsObj) {
            let oneSection = {};
            oneSection['data'] = sectionsObj[one];
            oneSection['sectionID'] = one;

            sections.push(oneSection);
        }
        return sections;
    }
}

export class Section {

    static sectionsAppend(sections, sectionIndex, list, selector) {
        let sectionList = getnode(sections, `${sectionIndex}.data`, []);
        let rowIndex = sectionList.length;

        for (row_index in list) {
            row = list[row_index];
            row['__row__'] = row_index + rowIndex;
            row['__section__'] = sectionIndex;
            Sections.cellType(row, selector);
        }
        return sectionList.push(...list);
    }

    static cellType(row, selector) {
        return Sections.cellType(row, selector);
    }
    // export default Sections


    static sectionsCreate(datas, selector) {
        let sectionsObj = Sections.load(datas, selector);
        let sections = new Array();

        for (one in sectionsObj) {
            let oneSection = {};
            oneSection['data'] = sectionsObj[one];
            oneSection['sectionID'] = one;

            sections.push(oneSection);
        }
        return sections;
    }

}